public class Test12 {
    public int rob(int[] nums) {
        int n = nums.length;
        if(n == 0) return 0;
        if(n == 1) return nums[0];
        if(n == 2) return Math.max(nums[0],nums[1]);
        int[] dp = new int[n];
        dp[1] = nums[0];
        dp[2] = nums[1];
        for(int i = 3;i < n;i++) {
            dp[i] = Math.max(dp[i - 2],dp[i - 3]) + nums[i - 1];
        }
        int value1 = dp[n - 1];
        int value2 = dp[n - 2];
        int value = value1 > value2 ? value1 : value2;
        dp[1] = nums[1];
        dp[2] = nums[2];
        for(int i = 3;i < n;i++) {
            dp[i] = Math.max(dp[i - 2],dp[i - 3]) + nums[i];
        }
        return Math.max(value,dp[n - 1]);

    }
}
